package com.parachutes.aspsystemweb.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.parachutes.aspinterfaceapi.domain.BusInterface;
import com.parachutes.aspinterfaceapi.domain.InterfaceFeign;
import com.parachutes.aspsystemapi.domian.Result;
import com.parachutes.aspsystemapi.domian.UserInterfaceSubscribe;
import com.parachutes.aspsystemapi.feign.UserInterfaceSubscribeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user-interface-subscribe")
public class UserInterfaceSubscribeController {

    @Resource
    private UserInterfaceSubscribeService userInterfaceSubscribeService;
    @Resource
    private InterfaceFeign interfaceFeign;

    @PostMapping("subscribe")
    public Result<?> subscribe(@Validated @RequestBody UserInterfaceSubscribe subscribe) {
        userInterfaceSubscribeService.save(subscribe);
        return Result.of(true, "订阅成功");
    }
    @PostMapping("unsubscribe")
    public Result<?> unsubscribe(@Validated @RequestBody UserInterfaceSubscribe subscribe) {
        userInterfaceSubscribeService.remove(Wrappers.lambdaQuery(UserInterfaceSubscribe.class).
                eq(UserInterfaceSubscribe::getUserId, subscribe.getUserId()).
                eq(UserInterfaceSubscribe::getInterfaceId, subscribe.getInterfaceId()));
        return Result.of(true, "取消订阅成功");
    }
    @GetMapping("subscribe-list")
    public Result<?> subscribeList(Integer page, Integer pageSize) {
        PageMethod.startPage(page, pageSize);
        List<UserInterfaceSubscribe> list = userInterfaceSubscribeService.list();
        List<Integer> collect = list.stream().map(UserInterfaceSubscribe::getInterfaceId).collect(Collectors.toList());
        if(collect.isEmpty()){
            return Result.of(true, "获取成功", new PageInfo<>(Collections.emptyList()));
        }
        String idsParam = String.join(",", collect.stream().map(String::valueOf).collect(Collectors.toList()));
        Result<PageInfo<BusInterface>> infoByIds = interfaceFeign.getInfoByIds(idsParam, page, pageSize);
        if (!infoByIds.isFlag()) {
            throw new RuntimeException(infoByIds.getMessage());
        }
        PageInfo<BusInterface> data = infoByIds.getData();
        return Result.of(true, "获取成功", data);
    }
    @PutMapping()
    public Result<?> interfaceList(@RequestBody UserInterfaceSubscribe subscribe) {
        Wrapper<UserInterfaceSubscribe> wrapper = Wrappers.lambdaUpdate(UserInterfaceSubscribe.class)
                .eq(UserInterfaceSubscribe::getUserId, subscribe.getUserId())
                .eq(UserInterfaceSubscribe::getInterfaceId, subscribe.getInterfaceId())
                        .set(UserInterfaceSubscribe::getInterfaceSecret, subscribe.getInterfaceSecret());
        userInterfaceSubscribeService.update(wrapper);
        return Result.of(true, "更新成功");
    }
}
